package com.ruoyi.system.service.impl;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.system.domain.TourismStatistics;
import com.ruoyi.system.mapper.TourismStatisticsMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.TourismAppointmentMapper;
import com.ruoyi.system.domain.TourismAppointment;
import com.ruoyi.system.service.ITourismAppointmentService;

import javax.annotation.Resource;

/**
 * 预约信息配置表Service业务层处理
 *
 * @author gqd
 * @date 2020-12-23
 */
@Service
public class TourismAppointmentServiceImpl implements ITourismAppointmentService
{
    @Autowired
    private TourismAppointmentMapper tourismAppointmentMapper;
    @Resource
    private TourismStatisticsMapper tourismStatisticsMapper;

    /**
     * 查询预约信息配置表
     *
     * @param id 预约信息配置表ID
     * @return 预约信息配置表
     */
    @Override
    public TourismAppointment selectTourismAppointmentById(Long id)
    {
        return tourismAppointmentMapper.selectTourismAppointmentById(id);
    }

    /**
     * 查询预约信息配置表列表
     *
     * @param tourismAppointment 预约信息配置表
     * @return 预约信息配置表
     */
    @Override
    public List<TourismAppointment> selectTourismAppointmentList(TourismAppointment tourismAppointment)
    {
        return tourismAppointmentMapper.selectTourismAppointmentList(tourismAppointment);
    }

    /**
     * 新增预约信息配置表
     *
     * @param tourismAppointment 预约信息配置表
     * @return 结果
     */
    @Override
    public int insertTourismAppointment(TourismAppointment tourismAppointment)
    {
        tourismAppointment.setCreateTime(DateUtils.getNowDate());
        return tourismAppointmentMapper.insertTourismAppointment(tourismAppointment);
    }

    /**
     * 修改预约信息配置表
     *
     * @param tourismAppointment 预约信息配置表
     * @return 结果
     */
    @Override
    public int updateTourismAppointment(TourismAppointment tourismAppointment)
    {
        tourismAppointment.setUpdateTime(DateUtils.getNowDate());
        return tourismAppointmentMapper.updateTourismAppointment(tourismAppointment);
    }

    /**
     * 批量删除预约信息配置表
     *
     * @param ids 需要删除的预约信息配置表ID
     * @return 结果
     */
    @Override
    public int deleteTourismAppointmentByIds(Long[] ids)
    {
        return tourismAppointmentMapper.deleteTourismAppointmentByIds(ids);
    }

    /**
     * 删除预约信息配置表信息
     *
     * @param id 预约信息配置表ID
     * @return 结果
     */
    @Override
    public int deleteTourismAppointmentById(Long id)
    {
        return tourismAppointmentMapper.deleteTourismAppointmentById(id);
    }

    /**
     * df.parse(format).before(df.parse(tourismAppointment.getEndTimes())) == true && stateTimes.equals(format1) &&
     * @param stateTimes
     * @return
     */
    @Override
    public AjaxResult selectAppointment(String stateTimes) {
        SimpleDateFormat df = new SimpleDateFormat("HH:mm");
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        String format = df.format(new Date());
        String format1 = sdf.format(new Date());
        List<TourismStatistics> tourismStatistics = tourismStatisticsMapper.selectByStateTimes(stateTimes);
        try {
            if(stateTimes.isEmpty()){
                List<TourismAppointment> tourismAppointments = tourismAppointmentMapper.selectAll();
                for(TourismAppointment tourismAppointment : tourismAppointments){
                    if(df.parse(format).before(df.parse(tourismAppointment.getEndTimes())) == true){
                        tourismAppointment.setState("1");
                    }else {
                        tourismAppointment.setState("0");
                    }
                }
                return AjaxResult.success(tourismAppointments);
            } else if(tourismStatistics.size()==0){
                List<TourismAppointment> tourismAppointments = tourismAppointmentMapper.selectAll();
                for(TourismAppointment tourismAppointment : tourismAppointments){
                    if(sdf.parse(format1).before(sdf.parse(stateTimes)) == true || stateTimes.equals(format1) && df.parse(format).before(df.parse(tourismAppointment.getEndTimes())) == true){
                        tourismAppointment.setState("1");
                    }else {
                        tourismAppointment.setState("0");
                    }
                }
                return AjaxResult.success(tourismAppointments);
        }else {
            List<TourismAppointment> tourismAppointments = tourismAppointmentMapper.selectAll();
            for(TourismStatistics statistics : tourismStatistics){
                Integer surplusSums = statistics.getSurplusSums();
                for(TourismAppointment appointment : tourismAppointments){
                    if(appointment.getId().equals(statistics.getAppointId())){
                        appointment.setPeopleNumber(surplusSums);
                    }
                    if(sdf.parse(format1).before(sdf.parse(stateTimes)) == true || stateTimes.equals(format1) && df.parse(format).before(df.parse(appointment.getEndTimes())) == true){
                        appointment.setState("1");
                    }else {
                        appointment.setState("0");
                    }
                }
            }
            return AjaxResult.success(tourismAppointments);
        }

        } catch (ParseException e) {
            e.printStackTrace();
        }
        return null;
    }
}
